<?php

namespace app\model;

use app\controller\wmapi\IndexController;
use support\Db;
use support\Model;

class WpUsedSpecsCans
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wp_python_used_specs_cans';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * wp二手车车型列表 - 分页
     * @return false|string
     */
    public static function list($page,$pageNum,$fields,$condition, $whereIn)
    {

        $offset = ($page - 1) * $pageNum;
        $model = Db::table('wp_python_used_specs_cans')
            ->select($fields)
            ->where($condition);
        if ($whereIn){
            $model = Db::table('wp_python_used_specs_cans')
                ->select($fields)
                ->where($condition)
                ->whereIn('series_id', $whereIn);
        }


        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('recommended_model', 'asc')
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);
        if ($list){
            foreach ($list as $k) {
                if ($k->fuel_form) {
                    if ($k->fuel_form == '柴油') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '汽油') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '油电混合') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '柴油') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '48V轻混系统') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '插电式混合动力') {
                        $k->is_nev = '1';
                        $k->is_nev_name = '新能源';
                    } elseif ($k->fuel_form == '压缩天然气(CNG)') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '增程式') {
                        $k->is_nev = '1';
                        $k->is_nev_name = '新能源';
                    } elseif ($k->fuel_form == '汽油电驱') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '氢燃料电池') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '醇电混合') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    } elseif ($k->fuel_form == '未知') {
                        $k->is_nev = '2';
                        $k->is_nev_name = '未知';
                    } elseif ($k->fuel_form == '纯电动') {
                        $k->is_nev = '1';
                        $k->is_nev_name = '新能源';
                    } else {
                        $k->is_nev = '2';
                        $k->is_nev_name = '传统能源';
                    }
                    $data = ['is_nev' => $k->is_nev, 'updated_at' => dateTime()];
                    $where = ['car_id' => $k->car_id];

                    $up = DcdSpecsCans::updated($where, $data);
                    if (!$up) {
                        echo 2222222222222;
                    }

                }

                if ($k->recommended_model == 1) {
                    $k->recommended_model_name = '推荐';
                }elseif($k->recommended_model == 2){
                    $k->recommended_model_name = '默认';
                }

            }
        }

        return json_encode(['list' => $list, 'count' => $count]);
    }


    /**
     * wp二手车车型列表 - 无分页
     * @param $page
     * @param $pageNum
     * @param $fields
     * @param $condition
     * @return false|string
     */
    public static function all($fields,$condition)
    {

        $list = Db::table('wp_python_used_specs_cans')
            ->select($fields)
            ->where($condition)
            ->orderBy('id', 'asc')
            ->get();
        foreach ($list as $k) {

        }
        return json_encode($list);
    }

    public static function find($condition)
    {
        $res = Db::table('wp_python_used_specs_cans')
            ->where($condition)
            ->first();
        if ($res){
        }
        return json_encode($res);
    }

    public static function created($data){

        return Db::table('wp_python_used_specs_cans')->insertGetId($data);

    }

    public static function updated($condition,$data){

        $res = Db::table('wp_python_used_specs_cans')->where($condition)->update($data);
        return json_encode($res);

    }

    public static function deleted($condition){

        $res = Db::table('wp_python_used_specs_cans')->where($condition)->delete();
        return json_encode($res);

    }



}


